<?php
namespace app\admin\service;
use think\Db;
use think\Loader;
class  Consul{


    //短信推送设置数据
    function consultSie(){
        $paginate = 15;
        $join     = [
            ['lt_admin b','a.admin_id=b.admin_id','LEFT'],
        ];
        $info  = Db::name('push_sie')->alias('a')->join($join)->field('a.*,b.a_fullname,b.a_phone')->order('a.cps_addtime desc')->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }

    //短信推送设置
    function consulPushRequestApi($a_username,$leave_type){
        if(empty($a_username)){
            return messageInfo('101','管理员必需填写');
        }
        $admin_id = Db::name('admin')->where('a_username',$a_username)->value('admin_id');
        if(count($admin_id) <= 0){
            return messageInfo('101','账号不存在');
        }
        $push_sie = Db::name('push_sie');
        $decide_errot     = $push_sie->where('admin_id',$admin_id)->count();
        if($decide_errot >= 1){
            return messageInfo('101','此账号已设置过了！');
        }
        $data_array['push_type']   = $leave_type;
        $data_array['admin_id']    = $admin_id;
        $data_array['cps_addtime'] = date('Y-m-d H:i:s');
        $a = $push_sie->insert($data_array);
        if(!$a){
            return messageInfo('101','设置失败！');
        }
        return messageInfo('202','设置成功！');
    }

    //短信推送设置，删除
    function consulPushDeleteApi($push_sie_id){
        if(empty($push_sie_id)){
            return messageInfo('101','系统参数错误');
        }
        $a = Db::name('push_sie')->where('consult_push_sie_id',$push_sie_id)->delete();
        if(!$a){
            return messageInfo('101','删除失败');
        }
        return messageInfo('202','删除成功');
    }




    //短信推送
    function dataPush($phone,$a_fullname,$details,$type=1){
        if ($type == 2) {
            $arrt['up_phone'] = $phone;
            $arrt['product']  = '咨询回复';
            $arrt['succeed']  = '回复成功';
            $arrt['errot']    = '回复失败6';
        }
        // 短信内容参数
    /*    Loader::import('Alidayu.AlidayuClient', EXTEND_PATH, '.class.php');
        Loader::import('Alidayu.Request.SmsNumSend', EXTEND_PATH, '.class.php');
        $client  = new \Alidayu\AlidayuClient();
        $request = new \Alidayu\Request\SmsNumSend ();
        $smsParams = array ('name' => $arrt['up_phone'],'details' => $details);
        // 设置请求参数
        $req  = $request->setSmsTemplateCode (config('AlidayuPush'))->setRecNum ( $arrt['up_phone'] )->setSmsParam ( json_encode ( $smsParams ) )->setSmsFreeSignName (config('AlidayuName'))->setSmsType ( 'normal' )->setExtend ( 'demo' );
        $data = $client->execute ( $req );

        if(!array_key_exists('alibaba_aliqin_fc_sms_num_send_response',$data) ){
            return messageInfo ( '101', $arrt['errot'] );
        }*/
        Loader::import('Aldy.Sms', EXTEND_PATH, '.class.php');
        $cofig ['templateCode'] = config('AlidayuID_HF');
        $sms = new \Sms($cofig);
        $smsParams = array ('name' => $arrt['up_phone'],'user'=>$a_fullname,'details'=>$details);
        $data = $sms->send_verify($arrt['up_phone'], json_encode ( $smsParams ));
        if ($data['Code'] == 'OK' && $data['Message'] == 'OK') {
            $response = messageInfo ( '202', $arrt['succeed'] );
        } else {
            $response = messageInfo ( '101', $arrt['errot'] );
        }
        return $response;
    }


}
